/**************************** (C) COPYRIGHT 2015 Fortiortech shenzhen *****************************
 * File Name          : TimerInit.c
 * Author             : Fortiortech  Market Dept
 * Version            : V1.0
 * Date               : 01/07/2015
 * Description        : This file contains Timer Initial function used for Motor Control.
 ***************************************************************************************************
 * All Rights Reserved
 **************************************************************************************************/

#include <FU68xx_4_MCU.h>
#include <MyProject.h>

void TIM3_Init_RF(void)
{
    ClrBit(PH_SEL, T3SEL);
    ClrBit(TIM3_CR1, T3EN);

    /*-------------------------------------------------------------------------------------------------
     (T3PSC)
     000:cpuclk			001:cpuclk/2^1	010:cpuclk/2^2	011:cpuclk/2^3
     100:cpuclk/2^4	101:cpucl/2^5	110:cpuclk/2^6	111:cpuclk/2^7
    -------------------------------------------------------------------------------------------------*/
    //  分频 / (1/最长时间ms*1000) < 65536，  不溢出即可   最长时间算长按的溢出时间
    //    SetReg(TIM3_CR0, T3PSC0 | T3PSC1 | T3PSC2, T3PSC1 );//6000KHz
    //  	SetReg(TIM3_CR0, T3PSC0 | T3PSC1 | T3PSC2, T3PSC1 | T3PSC0 );//3000KHz
    SetReg(TIM3_CR0, T3PSC0 | T3PSC1 | T3PSC2, T3PSC2); // 1500KHz
    //	  SetReg(TIM3_CR0, T3PSC0 | T3PSC1 | T3PSC2, T3PSC0 |T3PSC2);//750KHz

    ClrBit(TIM3_CR0, T3MOD); // 输入time模式
    ClrBit(TIM3_CR0, T3OCM); //

    ClrBit(TIM3_CR1, T3IR | T3IF | T3IP); //
    SetBit(TIM3_CR1, T3IPE | T3IFE);      //

    ClrBit(TIM3_CR1, T3NM1); //
    SetBit(TIM3_CR1, T3NM0); // 00-->???  01-->4cycles    10-->8cycles  11-->16cycles

    ClrBit(TIM3_CR0, T3OPM);

    PTIM31 = 0;
    PTIM30 = 0;
    EA = 1;

    TIM3__ARR = 0;
    TIM3__DR = 0;
    TIM3__CNTR = 0;
    SetBit(PH_SEL, T3SEL); // GP11

    SetBit(TIM3_CR1, T3EN);
}

/*-------------------------------------------------------------------------------------------------
    Function Name :	void TIM4_Init(void)
    Description   :	TIM4初始化配置
    Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/
void TIM4_Init(void)
{
    /*-------------------------------------------------------------------------------------------------
        先停止计数器，等配置完寄存器后，再重新启动计数器
-------------------------------------------------------------------------------------------------*/
    ClrBit(TIM4_CR1, T4EN); // 0-停止计数;1-使能计数
    ClrBit(PH_SEL, T4SEL);  // 0-不连接到端口GP11;1-连接到端口GP11
    /*-------------------------------------------------------------------------------------------------
        时钟分频设置(T5PSC)
      000:cpuclk			001:cpuclk/2^1	010:cpuclk/2^2	011:cpuclk/2^3
      100:cpuclk/2^4	101:cpuclk/2^5	110:cpuclk/2^6	111:cpuclk/2^7
    -------------------------------------------------------------------------------------------------*/
    SetReg(TIM4_CR0, T4PSC0 | T4PSC1 | T4PSC2, T4PSC0 | T4PSC2);

    /*-------------------------------------------------------------------------------------------------
        模式选择：
        0：输入timer模式
        1：输出模式
    -------------------------------------------------------------------------------------------------*/
    SetBit(TIM4_CR0, T4MOD); // 1，选择输出模式	;0，选择输入模式
    ClrBit(TIM4_CR0, T4OCM); // 0:上升沿触发，高电平位脉宽  1:下降沿触发，低电平为脉宽

    ClrBit(TIM4_CR1, T4IR | T4IF | T4IP); // 清除中断标志位
    SetBit(TIM4_CR1, T4IFE);              // 输入有效边沿变化中断使能和基本计时器上溢使能

    PTIM4S1 = 0;
    PTIM4S0 = 1; // TIM4/5中断优先级别为1
    EA = 1;

    TIM4__ARR = (uint32)TIM4_Fre / BuzzerRingFrq;
    TIM4__DR = ((uint32)TIM4_Fre >> 1) / BuzzerRingFrq; // 启动计数

    SetBit(TIM4_CR1, T4EN);
}

void TIM1ms_Init(void)
{
    // SYST_ARR = 24000;     //计算公式:24M/SYST_ARR;例 24M/24000=1000Hz
    PTIM4S1 = 0;
    PTIM4S0 = 1; // TIM4/5中断优先级设置为1
    EA = 1;
    SetBit(DRV_SR, SYSTIE); // 使能1ms定时中断
}
